package com.wz.boot.function.chatwithstranger;
import com.wz.boot.annotation.ExitMethod;
import com.wz.boot.annotation.Function;
import com.wz.boot.annotation.Handle;
import com.wz.boot.function.weather.Weather;
import love.forte.common.ioc.annotation.Beans;
import love.forte.common.ioc.annotation.Depend;
import love.forte.common.ioc.annotation.PostPass;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.Sender;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Friend;
import net.mamoe.mirai.contact.User;
import net.mamoe.mirai.data.UserProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;

@Function(
        priority = 5,
        name = "匿名聊天",
        describe = "和异性匿名聊天",
        touch = "匿名聊天",
        claName = "ChatWithStranger",
        execExit = true
)
@Beans
@Component
public class ChatWithStranger {
    private static Logger logger = LoggerFactory.getLogger(ChatWithStranger.class);

    private static final String MALE = UserProfile.Sex.MALE.name();
    private static final String FEMALE = UserProfile.Sex.FEMALE.name();
    private static final String UNKNOWN = UserProfile.Sex.UNKNOWN.name();

    //男性账号的列表
    private List<String> menAccountList = new ArrayList<>();
    //女性账号的列表
    private List<String> womenAccountList = new ArrayList<>();

    //已经配对好的列表 key:小哥哥qq号  value: 小姐姐qq号
    private Map<String,String> mateList = new HashMap<>();

    private Bot bot;
//    @PostPass(priority = 2)
//    public void initBot(){
//        List<Bot> instances = Bot.getInstances();
//        if(instances.size() != 0){
//
//            bot = instances.get(0);
//        }
//    }
    @Handle
    public void chat(PrivateMsg privateMsg, Sender sender){
        if(bot == null){
            bot = Bot.getInstances().get(0);
            logger.debug("已设置bot");
        }
        //得到此人的qq号
        String accountCode = privateMsg.getAccountInfo().getAccountCode();
        Friend friend = bot.getFriend(privateMsg.getAccountInfo().getAccountCodeNumber());
        UserProfile userProfile = friend.queryProfile();
        //得到此人的性别
        String sex = userProfile.getSex().name();
        logger.info("QQ号:{}, 性别:{}, 请求匿名匹配...",accountCode, sex);
        if(sex == null || sex.length() == 0 || UNKNOWN.equals(sex)){
            sender.sendPrivateMsg(accountCode,"图图暂时无法得知您的性别奥, 请设置您的QQ资料性别并设置其为他人可见, 才能为您匹配小哥哥小姐姐哟~");
            return;
        }
        //
        if(menAccountList.contains(accountCode) && sex.equals(FEMALE)){
            menAccountList.remove(accountCode);
        }
        if(womenAccountList.contains(accountCode) && sex.equals(MALE)){
            womenAccountList.remove(accountCode);
        }
        switch (sex){
            case "MALE":{
                if(menAccountList.contains(accountCode)){//在待匹配列表中
                    if(!womenAccountList.isEmpty()){
                        //为这个小哥哥随机选一个小姐姐聊天
                        String womenAccountCode = womenAccountList.get(new Random(System.currentTimeMillis()).nextInt(womenAccountList.size()));
                        //匹配成功, 从待匹配列表中移除小哥哥 小姐姐
                        menAccountList.remove(accountCode);
                        womenAccountList.remove(womenAccountCode);
                        //在配对好的列表中加入他
                        mateList.put(accountCode,womenAccountCode);
                        sender.sendPrivateMsg(accountCode, "匹配成功~试着和小姐姐聊天吧! 打招呼: 你好呀~");
                        sender.sendPrivateMsg(womenAccountCode,"匹配成功~试着和小哥哥聊天吧! 打招呼: 你好呀~");
                        logger.info("QQ号:{}, 性别:{}, 匿名匹配成功:",accountCode, sex, womenAccountCode);
                    }else{
                        //没有可以匹配的小姐姐
                        sender.sendPrivateMsg(accountCode, "不要着急, 当前系统中没有小姐姐哟~系统当前有"+menAccountList.size()+"个小哥哥,"+womenAccountList.size()+ "小姐姐奥~");
                        logger.info("QQ号:{}, 性别:{}, 不要着急, 当前系统中没有小姐姐哟~", accountCode, sex);
                    }
                }else{//不在待匹配列表中
                    //先看看这个小哥哥是否已经匹配成功
                    if(mateList.containsKey(accountCode)){
                        //已经又匹配好的小姐姐
                        //转发消息到小姐姐qq上
                        //得到小姐姐qq号
                        String womenAccountCode = mateList.get(accountCode);
                        sender.sendPrivateMsg(womenAccountCode,privateMsg.getMsgContent());
                    }else{
                        //小哥哥还没开始匹配呢
                        menAccountList.add(accountCode);
                        sender.sendPrivateMsg(accountCode,"正在为您匹配中~系统当前有"+menAccountList.size()+"个小哥哥,"+womenAccountList.size()+"小姐姐奥~");
                        //首次匹配
                        if(!womenAccountList.isEmpty()){
                            //为这个小哥哥随机选一个小姐姐聊天
                            String womenAccountCode = womenAccountList.get(new Random(System.currentTimeMillis()).nextInt(womenAccountList.size()));
                            //匹配成功, 从待匹配列表中移除小哥哥
                            menAccountList.remove(accountCode);
                            womenAccountList.remove(womenAccountCode);
                            //在配对好的列表中加入他
                            mateList.put(accountCode,womenAccountCode);
                            sender.sendPrivateMsg(accountCode, "匹配成功~试着和小姐姐聊天吧! 打招呼: 你好呀~");
                            sender.sendPrivateMsg(womenAccountCode,"匹配成功~试着和小哥哥聊天吧! 打招呼: 你好呀~");
                            logger.info("QQ号:{}, 性别:{}, 匿名匹配成功:",accountCode, sex, womenAccountCode);
                        }else{
                            //没有可以匹配的小姐姐
                            //sender.sendPrivateMsg(accountCode, "不要着急, 当前系统中没有小姐姐哟~");
                        }
                    }
                }
                break;
            }
            case "FEMALE":{
                if(womenAccountList.contains(accountCode)){//在待匹配列表中
                    if(!menAccountList.isEmpty()){
                        //为这个小姐姐随机选一个小哥哥聊天
                        String menAccountCode = menAccountList.get(new Random(System.currentTimeMillis()).nextInt(menAccountList.size()));
                        //匹配成功, 从待匹配列表中移除小姐姐和小哥哥
                        womenAccountList.remove(accountCode);
                        menAccountList.remove(menAccountCode);
                        //在配对好的列表中加入她
                        mateList.put(menAccountCode,accountCode);
                        sender.sendPrivateMsg(accountCode, "匹配成功~试着和小哥哥聊天吧! 打招呼: 你好呀~");
                        sender.sendPrivateMsg(menAccountCode,"匹配成功~试着和小姐姐聊天吧! 打招呼: 你好呀~");
                        logger.info("QQ号:{}, 性别:{}, 匿名匹配成功:",accountCode, sex, menAccountCode);
                    }else{
                        //没有可以匹配的小哥哥
                        sender.sendPrivateMsg(accountCode, "不要着急, 当前系统中没有小哥哥哟~ 系统当前有"+menAccountList.size()+"个小哥哥,"+womenAccountList.size()+"小姐姐奥~");
                        logger.info("QQ号:{}, 性别:{}, 不要着急, 当前系统中没有小哥哥哟~", accountCode, sex);
                    }
                }else{//不在待匹配列表中
                    //先看看这个小姐姐是否已经匹配成功
                    if(mateList.containsValue(accountCode)){
                        //已经又匹配好的小哥哥
                        //转发消息到小哥哥qq上
                        //得到小哥哥qq号
                        String menAccountCode = getKeyOfList(mateList,accountCode);
                        sender.sendPrivateMsg(menAccountCode,privateMsg.getMsgContent());
                    }else{
                        //小姐姐还没开始匹配呢
                        womenAccountList.add(accountCode);

                        sender.sendPrivateMsg(accountCode,"正在为您匹配中~ 系统当前有"+menAccountList.size()+"个小哥哥,"+womenAccountList.size()+"小姐姐奥~");
                        //首次匹配
                        if(!menAccountList.isEmpty()){
                            //为这个小姐姐随机选一个小哥哥聊天
                            String menAccountCode = menAccountList.get(new Random(System.currentTimeMillis()).nextInt(menAccountList.size()));
                            //匹配成功, 从待匹配列表中移除小姐姐和小哥哥
                            womenAccountList.remove(accountCode);
                            menAccountList.remove(menAccountCode);
                            //在配对好的列表中加入她
                            mateList.put(menAccountCode,accountCode);
                            sender.sendPrivateMsg(accountCode, "匹配成功~试着和小哥哥聊天吧! 打招呼: 你好呀~");
                            sender.sendPrivateMsg(menAccountCode,"匹配成功~试着和小姐姐聊天吧! 打招呼: 你好呀~");
                            logger.info("QQ号:{}, 性别:{}, 匿名匹配成功:",accountCode, sex, menAccountCode);
                        }
                    }
                }
                break;
            }
        }

        logger.info("当前匿名聊天待匹配列表中男性数: {}  女性数: {}", menAccountList.size(), womenAccountList.size());
        logger.info("当前已匹配列表中:" + mateList.toString());


    }

    @ExitMethod
    public void exit(PrivateMsg privateMsg, Sender sender){
        //得到此人的qq号
        String accountCode = privateMsg.getAccountInfo().getAccountCode();
        if(mateList.containsKey(accountCode)){
            //小哥哥要退出
            String womenAccountCode = mateList.get(accountCode);
            sender.sendPrivateMsg(womenAccountCode,"对方已退出匿名聊天");
            sender.sendPrivateMsg(accountCode,"您已退出匿名聊天");
            mateList.remove(accountCode);
        }
        else if(mateList.containsValue(accountCode)){
            //小姐姐要退出
            String menAccountCode = getKeyOfList(mateList,accountCode);
            sender.sendPrivateMsg(menAccountCode,"对方已退出匿名聊天");
            sender.sendPrivateMsg(accountCode,"您已退出匿名聊天");
            mateList.remove(menAccountCode);
        }


//        Friend friend = bot.getFriend(privateMsg.getAccountInfo().getAccountCodeNumber());
//        UserProfile userProfile = friend.queryProfile();
//        String sex = userProfile.getSex().name();
//        switch (sex){
//            case "MALE":{
//                if(mateList.containsKey(accountCode)){
//                    String womenAccountCode = mateList.get(accountCode);
//                    sender.sendPrivateMsg(womenAccountCode,"对方已退出匿名聊天");
//                    sender.sendPrivateMsg(accountCode,"您已退出匿名聊天");
//                    mateList.remove(accountCode);
//                }
//                break;
//            }
//            case "FEMALE":{
//                if(mateList.containsValue(accountCode)){
//                    String menAccountCode = getKeyOfList(mateList,accountCode);
//                    sender.sendPrivateMsg(menAccountCode,"对方已退出匿名聊天");
//                    sender.sendPrivateMsg(accountCode,"您已退出匿名聊天");
//                    mateList.remove(menAccountCode);
//                }
//                break;
//            }
//        }

    }


    public String getKeyOfList(Map<String, String> list, String value){
        for (String key : list.keySet()) {
            String val = list.get(key);
            if(value.equals(val)){
                return key;
            }
        }
        return null;
    }
}
